#include "ImageViews.h"

mini_engine::ImageViews *mini_engine::ImageViews::m_imageViewS = nullptr;
vector<VkImageView> mini_engine::ImageViews::swapChainImageViews;

mini_engine::ImageViews *mini_engine::ImageViews::getImageViews()
{
    if (m_imageViewS == nullptr)
    {
        m_imageViewS = new ImageViews();
        static ImageViewsDestruction imageViewsDestruction;
    }
    return m_imageViewS;
}

void mini_engine::ImageViews::createImageViews()
{
    swapChainImageViews.resize(SwapChain::swapChainImages.size());

    for (size_t i = 0; i < SwapChain::swapChainImages.size(); i++)
    {
        VkImageViewCreateInfo createInfo{};
        createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
        createInfo.image = SwapChain::swapChainImages[i];
        createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
        createInfo.format = SwapChain::swapChainImageFormat;
        createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
        createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
        createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
        createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
        createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
        createInfo.subresourceRange.baseMipLevel = 0;
        createInfo.subresourceRange.levelCount = 1;
        createInfo.subresourceRange.baseArrayLayer = 0;
        createInfo.subresourceRange.layerCount = 1;

        if (vkCreateImageView(LogicalDevice::logicalDevice, &createInfo, nullptr, &swapChainImageViews[i]) != VK_SUCCESS)
        {
            throw std::runtime_error("failed to create image views!");
        }
    }
}

mini_engine::ImageViews::ImageViews()
{
}

mini_engine::ImageViews::~ImageViews()
{
}

mini_engine::ImageViews::ImageViewsDestruction::~ImageViewsDestruction()
{
    if (m_imageViewS != nullptr)
    {
        delete m_imageViewS;
        m_imageViewS = nullptr;
    }
}